
package nju.bishe.weibo;

import java.util.List;

import nju.bishe.weibo.adapter.LaughStatusAdapter;
import nju.bishe.weibo.service.LaughChannelService;
import nju.bishe.weibo.ui.base.WithHeaderActivity;
import nju.bishe.weibo.ui.module.MenuDialog;

import weibo4andriod.Status;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextPaint;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * 
 * @author longwei.wu
 * @version $Id: LaughChannelActivity.java, v 0.1 2011-3-27 下午08:36:58 longwei.wu Exp $
 */
public class LaughChannelActivity extends Activity {
	private static final String TAG = "LaughChannelActivity";
    private ProgressDialog progressDialog;
    private LaughStatusAdapter laughStatusAdapter;
    private List<Status> laughStatus;
    private ListView listView;
    private ImageButton send_button;
    private ImageButton refresh_button;
    protected TextView titleButton;
    protected MenuDialog dialog;
//    private Button more_button;
    
	protected View mListHeader;
	protected View mListFooter;
	
	protected TextView loadMoreBtn;
	protected ProgressBar loadMoreGIF;
	protected TextView loadMoreBtnTop;
	protected ProgressBar loadMoreGIFTop;
	
	protected static int lastPosition = 0;

    private int page = 1;
    final LaughChannelService laughChannelService = new LaughChannelService();
    
    private final Handler handler = new Handler(){

        @Override
        public void handleMessage(android.os.Message msg) {
            progressDialog.dismiss();
            laughStatusAdapter = new LaughStatusAdapter(LaughChannelActivity.this,laughStatus);
            setupListHeader(true);
            listView.setAdapter(laughStatusAdapter);
            
        };
    };
    
    private final Handler refresh_Handler = new Handler(){
    	 @Override
         public void handleMessage(android.os.Message msg) {
    		 loadMoreGIF.setVisibility(View.GONE);
    		 
    		 laughStatusAdapter = new LaughStatusAdapter(LaughChannelActivity.this,laughStatus);
    		 listView.setAdapter(laughStatusAdapter);
    	 }
    };
    
    private final Handler more_handler = new Handler(){
        @Override
        public void handleMessage(android.os.Message msg) {
            laughStatusAdapter = new LaughStatusAdapter(LaughChannelActivity.this,laughStatus);
            
            listView.setAdapter(laughStatusAdapter);
        };
    };
    
    
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.laugh_channel);


        listView = (ListView) findViewById(R.id.laugh_channel_list);
        send_button = (ImageButton) findViewById(R.id.laugh_send_button);
        refresh_button = (ImageButton) findViewById(R.id.laugh_refresh_button);
//        more_button = (Button) findViewById(R.id.laugh_more_button);
        listView.setItemsCanFocus(false);
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    	addTitleButton();
    	setHeaderTitle("爆料");
        loadLaughStatus();
        listView.setOnItemClickListener(new OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long id) {
                // TODO Auto-generated method stub
            }
            
    });
        send_button.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LaughChannelActivity.this,WriteLaughChanelActivity.class));
            }
        });
        
        refresh_button.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                laughStatus = laughChannelService.getStatus(page);
            }
        });
//        
//        more_button.setOnClickListener(new OnClickListener() {
//            
//            @Override
//            public void onClick(View v) {
//                List<Status> more_laugh = laughChannelService.getStatus(page+1);
//                if(more_laugh.size()>0)
//                laughStatus.addAll(more_laugh);
//                more_handler.sendEmptyMessage(0);
//            }
//        });
  }
    @Override
    protected void onResume() {
        super.onResume();
        
    }
    
    private void loadLaughStatus(){

        this.progressDialog = ProgressDialog.show(this, " Working...", " 获得数据中", true, false);
        new Thread(){
            @Override
            public void run(){
                laughStatus = laughChannelService.getStatus(page);
//               laughChannelService.updateStatus("我在写android新浪微博，这是测试");
                System.out.println(laughStatus.size());
                handler.sendEmptyMessage(0);
            }
        }.start();
    }
    
    
protected void setupListHeader(boolean addFooter) {
        
        // Add Header to ListView
        mListHeader = View.inflate(this, R.layout.listview_header, null);
        listView.addHeaderView(mListHeader, null, true);
        mListHeader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadMoreGIFTop.setVisibility(View.VISIBLE);
                laughStatus = laughChannelService.getStatus(page);
                refresh_Handler.sendEmptyMessage(0);
            }
        });
        
        //TODO: 完成listView顶部和底部的事件绑定
        mListFooter = View.inflate(this, R.layout.listview_footer, null);
        listView.addFooterView(mListFooter, null, true);
        mListFooter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadMoreGIF.setVisibility(View.VISIBLE);
                List<Status> more_laugh = laughChannelService.getStatus(page+1);
                if(more_laugh.size()>0)
                laughStatus.addAll(more_laugh);
                more_handler.sendEmptyMessage(0);
            }
        });
        
        // Find View
        loadMoreBtn = (TextView)findViewById(R.id.ask_for_more);
        loadMoreGIF = (ProgressBar)findViewById(R.id.rectangleProgressBar);
        loadMoreBtnTop = (TextView)findViewById(R.id.ask_for_more_header);
        loadMoreGIFTop = (ProgressBar)findViewById(R.id.rectangleProgressBar_header);
        
        //loadMoreAnimation = (AnimationDrawable) loadMoreGIF.getIndeterminateDrawable();
    }

// LOGO按钮
protected void addTitleButton() {
	
	// Find View
	titleButton = (TextView) findViewById(R.id.laugh_title); 
	
	titleButton.setOnClickListener(new View.OnClickListener() {
		public void onClick(View v) {
			
			int top = titleButton.getTop();
			int height = titleButton.getHeight();
			int x = top + height;

			if (null == dialog) {
				Log.i(TAG, "Create menu dialog.");
				dialog = new MenuDialog(LaughChannelActivity.this);
				dialog.bindEvent(LaughChannelActivity.this);
				dialog.setPosition(-1, x);
			}
			
			// toggle dialog
			if (dialog.isShowing()) {
				dialog.dismiss(); //没机会触发
			} else {
				dialog.show();
			}
		}
	});
}

protected void setHeaderTitle(String title) {
	titleButton.setBackgroundDrawable( new BitmapDrawable());
	titleButton.setText(title);
	LayoutParams lp = new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
	lp.setMargins(3, 12, 0, 0);
	titleButton.setLayoutParams(lp);
	// 中文粗体
	TextPaint tp = titleButton.getPaint(); 
    tp.setFakeBoldText(true);
}
}
